package com.yanqu.road.server.manager.activity.biaoju;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.activity.biaoju.BiaoJuConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBiaoJuMgr extends TempCrossMgr {

    private static Map<Integer, CrossBiaoJuGroup> groupMap = new ConcurrentHashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        List<Integer> needRemoveList = new ArrayList<>();
        long currentSecond = DateHelper.getCurrentTime() / 1000;
        for (CrossBiaoJuGroup biaoJuGroup : groupMap.values()) {
            BiaoJuConfig biaoJuConfig = CrossBiaoJuConfigMgr.getBiaoJuConfig(biaoJuGroup.getActivityId());
            if (biaoJuConfig == null || currentSecond > biaoJuConfig.getActivityInfo().getEndShowTime() + 12 * DateHelper.HOUR_SECONDS) {
                needRemoveList.add(biaoJuGroup.getActivityId());
            }
        }

        for (Integer activityId : needRemoveList) {
            groupMap.remove(activityId);
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.BiaoJu.getValue());
        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
            Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
            if (null != activityGroupMap) {
                for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                    if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()) {
                        getActivity(activityGroup.getActivityId(), activityGroup.getGroupId());
                    }
                }
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (CrossBiaoJuGroup group : groupMap.values()) {
            group.save();
        }
        return true;
    }

    public static CrossBiaoJuGroup getBiaoJuGroup(int activityId) {
        CrossBiaoJuGroup group = groupMap.get(activityId);
        if (group == null) {
            synchronized (groupMap) {
                group = groupMap.get(activityId);
                if (group == null) {
                    BiaoJuConfig biaoJuConfig = CrossBiaoJuConfigMgr.getBiaoJuConfig(activityId);
                    if (biaoJuConfig == null) {
                        biaoJuConfig = CrossBiaoJuConfigMgr.reloadBiaoJuConfig(activityId);
                    }

                    if (biaoJuConfig == null) {
                        return null;
                    }

                    group = new CrossBiaoJuGroup(activityId);
                    groupMap.put(activityId, group);
                }
            }
        }
        return group;
    }

    public static CrossBiaoJuActivity getActivity(int activityId, int unionGroup) {
        CrossBiaoJuGroup group = getBiaoJuGroup(activityId);
        if (group == null) {
            return null;
        }
        return group.getActivity(unionGroup);
    }

    public static void runTask() {
        for (CrossBiaoJuGroup group : groupMap.values()) {
            group.runTask();
        }
    }

    public static void rankSortTask() {
        for (CrossBiaoJuGroup group : groupMap.values()) {
            group.rankSortTask();
        }
    }
}
